package com.foxzzz.base;

import com.alibaba.fastjson.JSON;

import java.util.LinkedList;
import java.util.List;


public class TreeNode {

    public int val;
    public TreeNode left;
    public TreeNode right;

   public  TreeNode(int x) {
        val = x;
    }


    public static TreeNode getTreeNode(String s) {
        List<Integer> list = (List<Integer>) JSON.parse(s);
        List<TreeNode> treeNodes = new LinkedList<>();
        for (Integer integer : list) {
            if (integer != null) {
                TreeNode t = new TreeNode(integer);
                treeNodes.add(t);
            } else {
                treeNodes.add(null);
            }
        }
        TreeNode root = treeNodes.get(0);
        for (int i = 1; i < treeNodes.size(); i++) {
            double realNum = (i - 1) / 2.0;
            int num = (i-1) / 2;
            if (num < realNum) {
                treeNodes.get(num).left = treeNodes.get(i);
            } else {
                treeNodes.get(num).right = treeNodes.get(i);
            }

        }
        return root;
    }


}
